#include "MainHeader.h"
int ResizeMap(RECT* Base){//Map gets 60% of window
//RECT newmap;
//RECT oldmap;
//GetWindowRect(GetDlgItem(hwndMain, idMap),oldmap);
RECT map;
float math1=0, math2=0;

map.top =0;
map.left =0; 
map.right = Base->right - Base->left;
map.bottom = Base->bottom - Base->top;
map.bottom -= 62.0;
map.right -= 370.0;
MoveWindow(GetDlgItem(hwndMain, idMap),0,0,map.right,map.bottom,0);
//Map->Resize();
MapWindow.Resize();
return 0;
}
int ResizeWorkArea(RECT* Base){

RECT rectWorkArea;
RECT Map;
RECT Test={0,0,0,0}; 
float math1;
GetWindowRect(GetDlgItem(hwndMain, idMap),&Map);
rectWorkArea.top =0;
rectWorkArea.left =Base->right-355 ; 
rectWorkArea.right =Base->right-rectWorkArea.left-25 ;
rectWorkArea.bottom = 175;
MoveWindow(GetDlgItem(hwndMain, idNavigation),rectWorkArea.left,rectWorkArea.top ,rectWorkArea.right,rectWorkArea.bottom,0);
WorkArea.Resize();
return 0;
}
int ResizeTools(RECT* Base){
RECT rTools;
RECT Map;
RECT WA;
float math1;
GetWindowRect(GetDlgItem(hwndMain, idMap),&Map);
GetWindowRect(GetDlgItem(hwndMain, idNavigation),&WA);
rTools.top =WA.bottom-WA.top;
rTools.left =Base->right-355 ; 
rTools.right =Base->right-rTools.left-25 ;
rTools.bottom = Base->bottom-Base->top -rTools.top-60 ;
MoveWindow(GetDlgItem(hwndMain, idToolset),rTools.left,rTools.top ,rTools.right,rTools.bottom,0);

Tools.Resize();
return 0;
}
int resizeall(HWND hwnd, RECT* rMain){
    GetWindowRect(hwnd, rMain);
    ResizeMap(rMain);
	ResizeWorkArea(rMain);
	ResizeTools(rMain);
	InvalidateRect(hwnd, NULL, 1);

	return 0;
}

BOOL CALLBACK   DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   char            FilePath[1024] =
   {
      0
   };
   long cmp=0;
   int i=0;
   RECT rMain, rMap, rTools, rWorking;
   char buffer[256]={0};//Used for random things 
   switch (message)
   {

   case WM_INITDIALOG:
       hwndMain=hwnd;
	   MapWindow.Init(GetDlgItem(hwndMain, idMap));
	   Tools.Init(GetDlgItem(hwndMain, idToolset));
	   WorkArea.Init(GetDlgItem(hwndMain, idNavigation));
	   resizeall( hwnd, &rMain);
      break;
   case BN_CLICKED:

      break;
   case WM_COMMAND:
   switch (LOWORD(wParam))
   {
       case IDCANCEL:
       EndDialog(hwnd, 0);
      
       break;
	case ID_chkLevel:
             WorkArea.chkBlackLayer.value(0);
			 WorkArea.chkClipdata.value(0);
			 WorkArea.chkMidLayer.value(0);
			 
			  break;
	case ID_chkMid:
             WorkArea.chkBlackLayer.value(0);
			 WorkArea.chkClipdata.value(0);
			 WorkArea.chkForeground.value(0);
			 
			  break;
	case ID_chkBack:
             WorkArea.chkMidLayer.value(0);
			 WorkArea.chkClipdata.value(0);
			 WorkArea.chkForeground.value(0);
			  break;
	case ID_chkMovement:
             WorkArea.chkMidLayer.value(0);
			 WorkArea.chkBlackLayer.value(0);
			 WorkArea.chkForeground.value(0);
			  break;
	case  ID_ROOM_SAVE:
		if(GBA.ROM){
            clsGame.Save();
			GBA.REDIT=fopen("C:\\tmp.bmp","r+w");
			if(GBA.REDIT){
                imgTileset.SaveToFile(GBA.REDIT);
				fclose(GBA.REDIT);
			}
		}

		break;
	   case ID_cboArea:
		   if(HIWORD(wParam)==CBN_SELCHANGE){//If user changes selectio
			   if(GBA.ROM){
	            
	               clsGame.curArea = WorkArea.cboArea.GetListIndex();
				   if(WorkArea.cboArea.GetListIndex() == 6){
					   while(1){
					   clsGame.curRoom = i;
						   if(clsGame.LoadHeader()==-1){
							   cmp=i-1;
							   break;
						   }
							i++;
					   }
				   }else{
                    cmp=clsGame.RoomOffsets[clsGame.curArea + 1]-clsGame.RoomOffsets[clsGame.curArea];

				   }
				  // clsGame.FullLoad();
				   WorkArea.cboRoom.Clear();
				   for(i=0;i<cmp;i++){
					   sprintf(buffer,"%x",i);
					   WorkArea.cboRoom.Additem(buffer);
				   }
                   WorkArea.cboRoom.SetListIndex(0);
				   clsGame.curRoom = 0;
                   SendMessage(hwnd, 0x111, 0x000103f4, 0x005b0a70);

			   }else{



				   MessageBox(hwnd,"Please load a ROM before playing with me!","ERROR",MB_OK);

			   }
		   }
		   break;
	   case ID_cboRoom:
	  if(HIWORD(wParam)==CBN_SELCHANGE){//If user changes selectio
				   if(GBA.ROM){
		             clsGame.curRoom = WorkArea.cboRoom.GetListIndex();
					 imgMap.Clear();
					 bbMap.Clear();
					 clsGame.FullLoad();
					 resizeall( hwnd, &rMain);
					 
				   }else{



					   MessageBox(hwnd,"Please load a ROM before playing with me!","ERROR",MB_OK);

				   }
			   }
		   break;
	   case ID_OPEN:
       GBA.ReturnFileName("Open a GBA ROM\0*.gba", GBA.FileLoc, 512);
       if (GBA.FileLoc[0])//Test to make sure it's active. 
	   {
         GBA.ROM = fopen(GBA.FileLoc, "r+b");
		 if(GBA.ROM){
         GBA.Seek(0xAC);//Wrapper for fseek
         GBA.Read(4,&cmp);
			 if(cmp==clsGame.ZMHeader){
			   clsGame.Game=0;
			   //Loaded ZM 
               clsGame.Setup(); 
			   
			   clsGame.curRoom=0;
			   clsGame.curArea=0;

               SendMessage(hwnd, 5, 0,0x020b0366);
                SendMessage(hwnd, 0x111, 0x000103f2, 0x0021185e);//emulate message to ID_cboArea
			 }else{
			    fclose(GBA.ROM);
                MessageBox(0,"Failed to open ROM.","None",MB_OK);//Fusion aint supported 
                return 0;
			 } 
		 }else{

		 MessageBox(0,"Failed to open ROM.","None",MB_OK);
          return 0;
	   }
	   }
		   break; 
   }
      break;

   case WM_VSCROLL: 
	   if((HWND)lParam==GetDlgItem(hwndMain,IDC_VSCROLLTILESET))  {
		   Tools.vScroll.UpdateScroll(wParam);
           InvalidateRect(Tools.hwndTileset,0,1);
	   }
	   if((HWND)lParam==GetDlgItem(hwndMain,ID_MAPVSCROLL))   {
  		   MapWindow.vScroll.UpdateScroll(wParam);
           InvalidateRect(MapWindow.hwndMap,0,1);
	   }
	       
 return 0;		


		

      break;
   case WM_HSCROLL:
	   if((HWND)lParam==GetDlgItem(hwndMain,ID_MAPHSCROLL))  {
		   MapWindow.hScroll.UpdateScroll(wParam); 
           InvalidateRect(MapWindow.hwndMap,0,1);
	   }      break;
   case WM_LBUTTONDOWN:


      break;


	case WM_SIZE:
     resizeall( hwnd, &rMain);
	break; 
   }

   return 0;
}

int WINAPI      WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{



   //HICON           hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON7));
                   hGlobal=hInstance;
				   GetCurrentDirectory(1023, AppPath);
                   DialogBox(hInstance, MAKEINTRESOURCE(idMain), 0, DialogProc);
                   if(GBA.ROM)fclose(GBA.ROM);//Make sure it's dead.

                   return 0;

}
